package jphysics;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Line;

/**
 * @author KaLeonis
 */
public class DCubicCurve extends Group {
	
	private CubicCurve shape;
	private Circle cs;
	private Circle ce;
	private Circle c1;
	private Circle c2;
	private Line l1;
	private Line l2;
	
	public DCubicCurve() {
		shape = new CubicCurve();
		shape.setStartX(500);
		shape.setStartY(100);
		shape.setControlX1(500);
		shape.setControlY1(200);
		shape.setControlX2(600);
		shape.setControlY2(200);
		shape.setEndX(600);
		shape.setEndY(100);
		shape.setFill(Color.BLACK);
		
		cs = new Circle(shape.getStartX(), shape.getStartY(), 5, Color.BLUE);
		ce = new Circle(shape.getEndX(), shape.getEndY(), 5, Color.BLUE);
		c1 = new Circle(shape.getControlX1(), shape.getControlY1(), 5, Color.BLUE);
		c2 = new Circle(shape.getControlX2(), shape.getControlY2(), 5, Color.BLUE);
		
		l1 = new Line(shape.getStartX(), shape.getStartY(), c1.getCenterX(), c1.getCenterY());
		l2 = new Line(shape.getEndX(), shape.getEndY(), c2.getCenterX(), c2.getCenterY());
		l1.setStroke(Color.GREY);
		l2.setStroke(Color.GREY);
		
		
		getChildren().add(shape);
		getChildren().add(l1);
		getChildren().add(l2);
		getChildren().add(cs);
		getChildren().add(ce);
		getChildren().add(c1);
		getChildren().add(c2);

		shape.setOnMouseDragged(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				shape_onMouseDragged(t);
			}
		});

		cs.setOnMouseDragged(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				cs_onMouseDragged(t);
			}
		});

		ce.setOnMouseDragged(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				ce_onMouseDragged(t);
			}
		});

		c1.setOnMouseDragged(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				c1_onMouseDragged(t);
			}
		});

		c2.setOnMouseDragged(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				c2_onMouseDragged(t);
			}
		});
	}
	
	private void shape_onMouseDragged(MouseEvent t) {
		double c1dX = shape.getControlX1() - shape.getStartX();
		double c1dY = shape.getControlY1() - shape.getStartY();
		
		double cedX = shape.getEndX() - shape.getStartX();
		double cedY = shape.getEndY() - shape.getStartY();
		
		double c2dX = shape.getControlX2() - shape.getStartX();
		double c2dY = shape.getControlY2() - shape.getStartY();
		
		shape.setStartX(t.getX());
		shape.setStartY(t.getY());
		
		shape.setControlX1(t.getX() + c1dX);
		shape.setControlY1(t.getY() + c1dY);
		
		shape.setEndX(t.getX() + cedX);
		shape.setEndY(t.getY() + cedY);
		
		shape.setControlX2(t.getX() + c2dX);
		shape.setControlY2(t.getY() + c2dY);

		cs.setCenterX(shape.getStartX());
		cs.setCenterY(shape.getStartY());

		c1.setCenterX(shape.getControlX1());
		c1.setCenterY(shape.getControlY1());

		ce.setCenterX(shape.getEndX());
		ce.setCenterY(shape.getEndY());

		c2.setCenterX(shape.getControlX2());
		c2.setCenterY(shape.getControlY2());

		l1.setStartX(cs.getCenterX());
		l1.setStartY(cs.getCenterY());
		l1.setEndX(c1.getCenterX());
		l1.setEndY(c1.getCenterY());

		l2.setStartX(ce.getCenterX());
		l2.setStartY(ce.getCenterY());
		l2.setEndX(c2.getCenterX());
		l2.setEndY(c2.getCenterY());
}

	private void cs_onMouseDragged(MouseEvent t) {
		cs.setCenterX(t.getX());
		cs.setCenterY(t.getY());
		shape.setStartX(cs.getCenterX());
		shape.setStartY(cs.getCenterY());
		l1.setStartX(cs.getCenterX());
		l1.setStartY(cs.getCenterY());
	}

	private void ce_onMouseDragged(MouseEvent t) {
		ce.setCenterX(t.getX());
		ce.setCenterY(t.getY());
		shape.setEndX(ce.getCenterX());
		shape.setEndY(ce.getCenterY());
		l2.setStartX(ce.getCenterX());
		l2.setStartY(ce.getCenterY());
	}
	
	private void c1_onMouseDragged(MouseEvent t) {
		c1.setCenterX(t.getX());
		c1.setCenterY(t.getY());
		shape.setControlX1(c1.getCenterX());
		shape.setControlY1(c1.getCenterY());
		l1.setEndX(c1.getCenterX());
		l1.setEndY(c1.getCenterY());
	}
	
	private void c2_onMouseDragged(MouseEvent t) {
		c2.setCenterX(t.getX());
		c2.setCenterY(t.getY());
		shape.setControlX2(c2.getCenterX());
		shape.setControlY2(c2.getCenterY());
		l2.setEndX(c2.getCenterX());
		l2.setEndY(c2.getCenterY());
	}
}
